package com.hd.trans.utils

import androidx.appcompat.app.AppCompatActivity
import com.hd.trans.db.bean.MediaItem
import com.hd.trans.files.bean.Album
import com.hd.trans.files.bean.MediaOptions
import com.hd.trans.files.bean.MimeType
import com.hd.trans.loader.MediaPresenter

object AlbumHelper {

    fun getLatestPhoto(
        context: AppCompatActivity,
        all: Boolean = false,
        mimeTypes: ArrayList<MimeType>? = null,
        excludedMimeTypes: ArrayList<MimeType>? = null,
        block:(String)->Unit){

        val options = MediaOptions.build(100).setMax(1).setMin(1).setMediaType(MediaOptions.TYPE_IMAGE)
        if (mimeTypes != null) {
            options.setIncludeMimeType(mimeTypes)
        } else {
            if (all) {
                if (excludedMimeTypes != null) {
                    options.setExcludeMimeType(excludedMimeTypes)
                }
            } else {
                options.setIncludeMimeType(MimeType.JPEG, MimeType.PNG, MimeType.HEIC)
            }
        }

        val mediaCallback = object : MediaPresenter.MediaCallback{
            override fun onAlbumLoading() { }

            override fun onMediaResult(mediaItemList: MutableList<MediaItem>?) {
                val mediaItem = mediaItemList?.firstOrNull()
                if (mediaItem == null){
                    block.invoke("")
                }
                else{
                    block.invoke(mediaItem.path)
                }
            }
        }

        val mediaPresenter = MediaPresenter()
        mediaPresenter.onCreate(context, mediaCallback, options)

        mediaPresenter.load(Album())
    }

}